package com.kd.xjoa.scheduled.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kd.xjoa.scheduled.search.bean.IndexParam;
import com.kd.xjoa.scheduled.search.bean.IndexResult;
import com.kd.xjoa.scheduled.search.utils.IndexSearchUtil;

public class ScheduledCommonUtil {
	
	private static final Log logger = LogFactory.getLog(ScheduledCommonUtil.class);

	public static void initDateTime(IndexParam param,String snapDate) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		Long startTime = null;
		Long endTime = null;
		try {
			date = sdf.parse(snapDate);
			startTime = sdf2.parse(sdf1.format(date)+" 00:00:00").getTime();
			endTime = sdf2.parse(sdf1.format(date)+" 23:59:59").getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		param.setStartTime(startTime);
		param.setEndTime(endTime);
	}
	
	public static List<Date> getBetweenDates(Date begin, Date end) {
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        /* Calendar tempEnd = Calendar.getInstance();
        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }*/
        while(begin.getTime()<=end.getTime()){
	         result.add(tempStart.getTime());
	         tempStart.add(Calendar.DAY_OF_YEAR, 1);
	         begin = tempStart.getTime();
	     }
        return result;
	}
	
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		List<Date> list = getBetweenDates(sdf.parse("20170606"), sdf.parse("20170606"));
		for (Date date : list) {
			System.out.println(sdf.format(date));
		}
	}
	
	
	public static int indexSearch(IndexParam param){
		//初始化查询时间
		param.setPageNum(1);
		param.setPageSize(1);
		//搜索
		IndexResult result = IndexSearchUtil.search(param);
		if(result == null){
			logger.error("index search error, result null ");
			return -1;
		} 
		if(result.getCode() != 200){
			logger.error("index search error, " + result.toString());
			return -1;
		}
		return result.getData()!=null?result.getData().getTotalElements():-1;
	}
}
